home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr07 / oleo130s.zip / OLEO130S.TAR / oleo-1.3 / line.c < prev    next >
C/C++ Source or Header  |  1993-03-25  |  4KB  |  233 lines

  1. #include <stdio.h>
  2. #include "global.h"
  3. #include "line.h"
  4.  
  5. #ifdef __STDC__
  6. void
  7. set_line (struct line *line, char *string)
  8. #else
  9. void
  10. set_line (line, string)
  11.      struct line *line;
  12.      char *string;
  13. #endif
  14. {
  15.   int len;
  16.  
  17.   len = strlen (string);
  18.   if (line->alloc <= len)
  19.     {
  20.       if (len < LINE_MIN)
  21.     len = LINE_MIN;
  22.       else
  23.     len++;
  24.       line->alloc = len + 1;
  25.       if (line->buf)
  26.     line->buf = ck_realloc (line->buf, line->alloc);
  27.       else
  28.     line->buf = ck_malloc (line->alloc);
  29.     }
  30.   strcpy (line->buf, string);
  31. }
  32.  
  33. #ifdef __STDC__
  34. void
  35. setn_line (struct line *line, char *string, int n)
  36. #else
  37. void
  38. setn_line (line, string, n)
  39.      struct line *line;
  40.      char *string;
  41.      int n;
  42. #endif
  43. {
  44.   int len = n;
  45.   if (line->alloc <= len)
  46.     {
  47.       if (len < LINE_MIN)
  48.     len = LINE_MIN;
  49.       else
  50.     len++;
  51.       line->alloc = len;
  52.       line->buf = ck_remalloc (line->buf, line->alloc);
  53.     }
  54.   strcpy (line->buf, string);
  55. }
  56.  
  57. #define Max(A,B)  ((A) > (B) ? (A) : (B))
  58.  
  59. #ifdef __STDC__
  60. void
  61. catn_line (struct line *line, char *string, int n)
  62. #else
  63. void
  64. catn_line (line, string, n)
  65.      struct line *line;
  66.      char *string;
  67.      int n;
  68. #endif
  69. {
  70.   int len = (line->buf ? strlen (line->buf) : 0);
  71.   if (line->alloc <= len + n + 1)
  72.     {
  73.       line->alloc = Max (len + n + 1, LINE_MIN);
  74.       line->buf = ck_remalloc (line->buf, line->alloc);
  75.     }
  76.   bcopy (string, line->buf + len, n);
  77.   line->buf[len + n] = '\0';
  78. }
  79.  
  80.  
  81. #ifdef __STDC__
  82. void
  83. sprint_line (struct line *line, char * fmt, ...)
  84. #else
  85. void
  86. sprint_line (line, fmt, va_alist)
  87.      struct line *line;
  88.      char *fmt;
  89.      va_dcl
  90. #endif
  91. {
  92.   va_list iggy;
  93.   int len;
  94.  
  95.   len = strlen (fmt) + 200;
  96.   if (!line->alloc)
  97.     {
  98.       line->buf = ck_malloc (len);
  99.       line->alloc = len;
  100.     }
  101.   else if (line->alloc < len)
  102.     {
  103.       line->buf = ck_realloc (line->buf, len);
  104.       line->alloc = len;
  105.     }
  106.   var_start (iggy, fmt);
  107.   vsprintf (line->buf, fmt, iggy);
  108.   va_end (iggy);
  109. }
  110.  
  111. #ifdef __STDC__
  112. void
  113. splicen_line (struct line * line, char * str, int n, int pos)
  114. #else
  115. void
  116. splicen_line (line, str, n, pos)
  117.      struct line * line;
  118.      char * str;
  119.      int n;
  120.      int pos;
  121. #endif
  122. {
  123.   int old_len = strlen (line->buf);
  124.   int len = old_len + n;
  125.   if (line->alloc <= len)
  126.     {
  127.       line->alloc = len;
  128.       line->buf = ck_remalloc (line->buf, len + 1);
  129.     }
  130.   line->buf[len--] = '\0';
  131.   --old_len;
  132.   while (old_len >= pos)
  133.     {
  134.       line->buf[len] = line->buf[old_len];
  135.       --len;
  136.       --old_len;
  137.     }
  138.   while (n--)
  139.     line->buf[pos + n] = str[n];
  140. }
  141.  
  142. #ifdef __STDC__
  143. void
  144. edit_line (struct line * line, int begin, int len)
  145. #else
  146. void
  147. edit_line (line, begin, len)
  148.      struct line * line;
  149.      int begin;
  150.      int len;
  151. #endif
  152. {
  153.   int old_len = strlen (line->buf);
  154.   int new_len = old_len - len;
  155.   while (begin < new_len)
  156.     {
  157.       line->buf[begin] = line->buf[begin + len];
  158.       ++begin;
  159.     }
  160.   line->buf[begin] = '\0';
  161. }
  162.  
  163.  
  164. #ifdef __STDC__
  165. void
  166. free_line (struct line * line)
  167. #else
  168. void
  169. free_line (line)
  170.      struct line * line;
  171. #endif
  172. {
  173.   if (line->buf && line->alloc)
  174.     free (line->buf);
  175.   line->buf = 0;
  176.   line->alloc = 0;
  177. }
  178.  
  179.  
  180.  
  181.  
  182. #ifdef __STDC__
  183. int
  184. read_line (struct line * line, FILE * fp, int * linec)
  185. #else
  186. int
  187. read_line (line, fp, linec)
  188.      struct line * line;
  189.      FILE * fp;
  190.      int * linec;
  191. #endif
  192. {
  193.   int pos = 0;
  194.   int c = getc (fp);
  195.  
  196.   while ((c != EOF) && (c != '\n'))
  197.     {
  198.       if (pos + 2 >= line->alloc)
  199.     {
  200.       line->alloc = (line->alloc ? line->alloc * 2 : 1);
  201.       line->buf = ck_remalloc (line->buf, line->alloc);
  202.     }
  203.       if (c != '\\')
  204.     line->buf[pos++] = c;
  205.       else
  206.     {
  207.       int next_c = getc (fp);
  208.       if (next_c != '\n')
  209.         {
  210.           line->buf[pos++] = '\\';
  211.           line->buf[pos++] = next_c;
  212.         }
  213.       /* Else the backslash and newline are discarded from the input. */
  214.       else
  215.         ++*linec;
  216.     }
  217.       c = getc (fp);
  218.     }
  219.   if (pos + 1 > line->alloc)
  220.     {
  221.       ++line->alloc;
  222.       line->buf = ck_remalloc (line->buf, line->alloc);
  223.     }
  224.   line->buf[pos] = 0;
  225.   if (line->buf[0] || (c != EOF))
  226.     {
  227.       ++*linec;
  228.       return 1;
  229.     }
  230.   else
  231.     return 0;
  232. }
  233.